var common = require('../../../utils/common.js');
var app = getApp();
var baseUrl = app.url;

Page({
    data: {
        datalist:{},
        address:'',
        inputVal:"",
        __PUBLIC__:app.__PUBLIC__  
    },
    onLoad: function (options) {
        common.bestCurl('GET','/agency/detail',res=>{
            if(res.statusCode == 200){
                if(res.data.code==2){
                    console.log(res.data)
                }else{
                   this.setData({details:res.data,lat:res.data.lat,lng:res.data.lng,address:res.data.address})
                }
            }
        },{'id':options.id})
    },
    select:function (event){
        var that = this;
        wx.chooseLocation({
            success: function(res) {
                var latitude = res.latitude
                var longitude = res.longitude
                var address = res.address
                that.setData({address:res.address,latitude:res.latitude,longitude:res.longitude})
            }
        }) 
    },
    formSubmit:function (event) {
        var lng = this.data.longitude;
        var lat = this.data.latitude;
        var formData = event.detail.value;
        common.bestCurl('POST','/agency/edit',res=>{
            if(res.statusCode == 200){
                if(res.data.code == 1){
                    wx.showToast({
                        title: '更新成功',
                        icon: '',
                        duration: 5000
                    });
                    setTimeout(res=>{
                        wx.redirectTo({
                            url: '../list/list'
                        })
                    },2000)
                }else if(res.data.code == 2){
                    wx.showModal({
                        content: '更新失败,请重新更新',
                        showCancel: false,
                    });
                }
            }
        },Object.assign({lng:lng},{lat:lat},formData))            
    }
});

